package controllers.Administrador;

import java.util.Collection;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import services.ComunidadDeVecinosService;
import services.PagoService;
import controllers.AbstractController;
import domain.ComunidadDeVecinos;
import domain.Pago;

@Controller
@RequestMapping("/pago/administrator")
public class PagoAdministratorController extends AbstractController {

	@Autowired
	private PagoService pagoService;

	@Autowired
	private ComunidadDeVecinosService comunidadDeVecinosService;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ModelAndView list(@RequestParam int comunidadId) {
		ModelAndView result;
		Collection<Pago> pagos;

		pagos = pagoService.findPagosByComunidadDeVecinos(comunidadId);

		result = new ModelAndView("pago/list");
		result.addObject("requestURI", "pago/administrator/list.do");
		result.addObject("pagos", pagos);
		result.addObject("comunidadId", comunidadId);
		result.addObject("backURI",
				"/comunidad/administrator/edit.do?comunidadId=" + comunidadId);

		return result;
	}

	public PagoAdministratorController() {
		super();
	}

	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public ModelAndView create(@RequestParam int comunidadId) {
		ModelAndView result;
		ComunidadDeVecinos cv;
		Pago pago;
		Boolean isNew;

		cv = comunidadDeVecinosService.findOne(comunidadId);
		pago = pagoService.create(cv);
		isNew = true;

		result = new ModelAndView("pago/create");
		result.addObject("requestURI", "pago/administrator/edit.do");
		result.addObject("pago", pago);
		result.addObject("isNew", isNew);
		result.addObject("backURI", "/pago/administrator/list.do?comunidadId="
				+ comunidadId);
		return result;
	}

	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public ModelAndView edit(@RequestParam int pagoId) {
		ModelAndView result;
		Pago pago;
		Boolean isNew;

		pago = pagoService.findOne(pagoId);
		isNew = false;

		result = new ModelAndView("pago/edit");
		result.addObject("requestURI", "pago/administrator/edit.do");
		result.addObject("pago", pago);
		result.addObject("isNew", isNew);
		result.addObject("backURI", "/pago/administrator/list.do?comunidadId="
				+ pago.getComunidadDeVecinos().getId());
		return result;
	}

	@RequestMapping(value = "/pagar", method = RequestMethod.GET)
	public ModelAndView pagar(@RequestParam int pagoId) {
		ModelAndView result;
		Pago pago;
		Boolean isNew;

		pago = pagoService.findOne(pagoId);
		isNew = false;
		try {
			Boolean hayFondo;
			hayFondo = pagoService.hayFondoParaPagar(pagoId);

			if (hayFondo) {
				result = new ModelAndView("pago/edit");
				pagoService.pagar(pagoId);

			} else {
				result = createEditModelAndView(pago, "pago.noHayFondo.error");
			}
		} catch (Throwable oops) {
			result = createEditModelAndView(pago, "pago.pagar.error");
			return result;

		}

		result.addObject("requestURI", "pago/administrator/edit.do");
		result.addObject("pago", pago);
		result.addObject("isNew", isNew);
		result.addObject("backURI", "/pago/administrator/list.do?comunidadId="
				+ pago.getComunidadDeVecinos().getId());
		return result;
	}

	@RequestMapping(value = "/edit", method = RequestMethod.POST, params = "save")
	public ModelAndView createSave(@Valid Pago pago, BindingResult binding) {
		ModelAndView result;
		Boolean isNew;

		if (binding.hasErrors()) {
			result = createEditModelAndView(pago);
		} else {
			try {

				pagoService.save(pago);
				result = new ModelAndView(
						"redirect:/pago/administrator/list.do?comunidadId="
								+ pago.getComunidadDeVecinos().getId());
			} catch (Throwable oops) {

				result = createEditModelAndView(pago, "pago.commit.error");
			}
		}

		if (pago.getId() == 0) {
			isNew = true;
		} else {
			isNew = false;
		}
		result.addObject("requestURI", "pago/administrator/edit.do");

		result.addObject("backURI", "/pago/administrator/list.do?comunidadId="
				+ pago.getComunidadDeVecinos().getId());
		result.addObject("isNew", isNew);
		return result;
	}

	// Ancillary methods
	protected ModelAndView createEditModelAndView(Pago pago) {
		ModelAndView result;

		result = createEditModelAndView(pago, null);

		return result;
	}

	protected ModelAndView createEditModelAndView(Pago pago, String message) {
		ModelAndView result;
		Boolean isNew;
		result = new ModelAndView("pago/create");

		if (pago.getId() == 0) {
			isNew = true;
			result.addObject("requestURI", "pago/administrator/edit.do");
		} else {
			result.addObject("requestURI", "pago/administrator/edit.do");
			isNew = false;
		}
		result.addObject("pago", pago);
		result.addObject("isNew", isNew);
		result.addObject("message", message);

		return result;
	}
}
